package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.SpellUtils;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.PracticeBase;
import com.ruoyi.system.mapper.PracticeBaseMapper;
import com.ruoyi.system.service.IPracticeBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基层实践单位信息Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class PracticeBaseServiceImpl implements IPracticeBaseService {
    @Autowired
    private PracticeBaseMapper practiceBaseMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询基层实践单位信息
     *
     * @param baseId 基层实践单位信息主键
     * @return 基层实践单位信息
     */
    @Override
    public PracticeBase selectPracticeBaseByBaseId(Long baseId) {
        return practiceBaseMapper.selectPracticeBaseByBaseId(baseId);
    }

    /**
     * 查询基层实践单位信息列表
     *
     * @param practiceBase 基层实践单位信息
     * @return 基层实践单位信息
     */
    @Override
    public List<PracticeBase> selectPracticeBaseList(PracticeBase practiceBase) {
        return practiceBaseMapper.selectPracticeBaseList(practiceBase);
    }

    /**
     * 新增基层实践单位信息
     *
     * @param practiceBase 基层实践单位信息
     * @return 结果
     */
    @Override
    public int insertPracticeBase(PracticeBase practiceBase) {
        practiceBase.setCreateTime(DateUtils.getNowDate());
        return practiceBaseMapper.insertPracticeBase(practiceBase);
    }

    /**
     * 修改基层实践单位信息
     *
     * @param practiceBase 基层实践单位信息
     * @return 结果
     */
    @Override
    public int updatePracticeBase(PracticeBase practiceBase) {
        return practiceBaseMapper.updatePracticeBase(practiceBase);
    }

    /**
     * 批量删除基层实践单位信息
     *
     * @param baseIds 需要删除的基层实践单位信息主键
     * @return 结果
     */
    @Override
    public int deletePracticeBaseByBaseIds(Long[] baseIds) {
        return practiceBaseMapper.deletePracticeBaseByBaseIds(baseIds);
    }

    /**
     * 删除基层实践单位信息信息
     *
     * @param baseId 基层实践单位信息主键
     * @return 结果
     */
    @Override
    public int deletePracticeBaseByBaseId(Long baseId) {
        return practiceBaseMapper.deletePracticeBaseByBaseId(baseId);
    }

    /**
     * 实体数据补全
     *
     * @param practiceBase 实体
     * @param userId       用户id
     * @param crateType    创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(PracticeBase practiceBase, Long userId, Integer crateType) {
        //主键
        if (practiceBase.getBaseId() == null || "".equals(practiceBase.getBaseId())) {
            practiceBase.setBaseId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //编码
        if (practiceBase.getBaseCode() == null || "".equals(practiceBase.getBaseCode())) {
            practiceBase.setBaseCode(SpellUtils.convertSpell(practiceBase.getBaseName(), false));
        }
        //描述
        if (practiceBase.getBaseDesc() == null) {
            practiceBase.setBaseDesc("");
        }
        //是否有效
        if (practiceBase.getIsValid() == null) {
            practiceBase.setIsValid(1L);
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (practiceBase.getCreateBy() == null) {
                practiceBase.setCreateBy(userId.toString());
            }
            if (practiceBase.getCreateTime() == null) {
                practiceBase.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (practiceBase.getModifyBy() == null) {
            practiceBase.setModifyBy(userId);
        }
        //修改时间
        if (practiceBase.getModifyTime() == null) {
            practiceBase.setModifyTime(DateUtils.getNowDate());
        }
    }
}
